<template>
  <j-editable-table
     ref="sdSalesDetailOutTable"
    :loading="loading"
    :columns="columns"
    :dataSource="dataSource"
    :maxHeight="300"
    :disabled="false"
    :rowNumber="true"
    :rowSelection="false"
    :actionButton="false"/>
</template>

<script>

import {FormTypes} from '@/utils/JEditableTableUtil'
import {JEditableTableModelMixin} from '@/mixins/JEditableTableModelMixin'
import {getAction, httpAction, putAction} from "@/api/manage";

export default {
  name: 'SdSalesDetailOutTable',
  mixins: [JEditableTableModelMixin],
  components: {},
  data() {
    return {
      loading: false,
      dataSource: [],
      columns: [
        {
          title: '销单号',
          key: 'orderNo',
          type: FormTypes.inputNumber,
          width: "120px",
          defaultValue: '',
          disabled: true,
        },
        {
          title: '行号',
          key: 'lineNo',
          type: FormTypes.inputNumber,
          width: "60px",
          defaultValue: '',
          disabled: true,
        },
        {
          title: '客户',
          key: 'customerId',
          type: FormTypes.select,
          dictCode:"crm_customer,cust_full_name,id",
          width: "200px",
          defaultValue: '',
          disabled: true,
        },
        {
          title: '物料号',
          key: 'materialNo',
          type: FormTypes.input,
          width: "120px",
          defaultValue: '',
          disabled: true,
        },
        {
          title: '物料描述',
          key: 'materialDesc',
          type: FormTypes.input,
          width: "200px",
          defaultValue: '',
          disabled: true,
        },
        {
          title: '单位',
          key: 'unit',
          type: FormTypes.select,
          dictCode:"mdm_unit,unit_name,unit_code",
          width:"100px",
          defaultValue:'',
          disabled: true
        },
        {
          title: '出库数量',
          key: 'outQty',
          type: FormTypes.inputNumber,
          width: "100px",
          placeholder: '请输入${title}',
          defaultValue: '0',
          validateRules: [
            {
              handler(type, value, row, column, callback, target) {
                let {values} = target.getValuesSync({validate: false, rowIds: [row.id]});
                if (type === 'blur' || type === 'getValues') {
                  if (value < 0) {
                    callback(false, '请输入大于零的数')
                    return;
                  } else if (value > Number(values[0].stockQtyAvailable)) {
                    callback(false, '${title}不能超出可用库存量：' + values[0].stockQtyAvailable)
                    return;
                  } else if (value > Number(values[0].undeliveryQty)) {
                    callback(false, '${title}不能超出可交货数量：' + values[0].undeliveryQty)
                    return;
                  } else {
                    callback(true)
                  }
                } else {
                  callback();
                }
              },
            },
          ],
        },
        {
          title: '可交货数量',
          key: 'undeliveryQty',
          type: FormTypes.inputNumber,
          width: "100px",
          defaultValue: '0',
          disabled: true
        },
        {
          title: '可用库存量',
          key: 'stockQtyAvailable',
          type: FormTypes.inputNumber,
          width: "100px",
          defaultValue: '0',
          disabled: true
        },
        {
          title: '订单数量',
          key: 'qty',
          type: FormTypes.inputNumber,
          width: "100px",
          defaultValue: '0',
          disabled: true
        },
        {
          title: '已交货数量',
          key: 'deliveryQty',
          type: FormTypes.inputNumber,
          width: "100px",
          defaultValue: '0',
          disabled: true
        },
        {
          title: '项目',
          key: 'projectId_dictText',
          type: FormTypes.input,
          width: "200px",
          defaultValue: '',
          disabled: true,
        },
        {
          title: '合同号',
          key: 'contractNo',
          type: FormTypes.input,
          width: "160px",
          defaultValue: '',
          disabled: true,
        },
        {
          title: '库房',
          key: 'warehouseId',
          type: FormTypes.sel_search,
          dictCode:"mdm_warehouse,warehouse_name,id",
          width:"140px",
          defaultValue:'',
          disabled: true
        },
        {
          title: '库位',
          key: 'storageLocationId',
          type: FormTypes.sel_search,
          dictCode:"mdm_storage_location,location_name,id",
          width:"140px",
          defaultValue:'',
          disabled: true
        },
        {
          title: '批次',
          key: 'batchNo',
          type: FormTypes.input,
          width: "140px",
          defaultValue: '',
          disabled: true,
        },
        {
          title: '含税单价',
          key: 'unitPrice',
          type: FormTypes.inputNumber,
          width: "100px",
          defaultValue: '0',
          disabled: true,
        },
        {
          title: '含税金额',
          key: 'sumPrice',
          type: FormTypes.inputNumber,
          width: "100px",
          placeholder: '请输入${title}',
          defaultValue: '0',
          disabled: true,
        },
        {
          title: '物料ID',
          key: 'materialId',
          type: FormTypes.hidden,
        },
        {
          title: '销单明细ID',
          key: 'salesOrderDetailId',
          type: FormTypes.hidden,
        },
        {
          title: '销单ID',
          key: 'salesOrderId',
          type: FormTypes.hidden,
        },
        {
          title: '项目',
          key: 'projectId',
          type: FormTypes.hidden,
          defaultValue: '',
        },
        {
          title: '合同ID',
          key: 'contractId',
          type: FormTypes.hidden,
          defaultValue: '',
        },
      ],
      url: {
        handleOut: "/sd/salesOrder/handleOut",
        materialStockList: "/mm/materialStock/availableList",
      }
    }
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
  },
  methods: {
    edit(recordArr){
      let that = this
      this.loading = true;
      let materialIds = recordArr.map(item => item.materialId).join();
      getAction(this.url.materialStockList, {materialIds: materialIds, pageSize: 500}).then((res) => {
        let dataArr = [];
        if (res.success) {
          let result = res.result.records||res.result;
          recordArr.forEach(
            (record) => {
              result.forEach((item,index) => {
                if(item.materialId == record.materialId){
                  let stockData = {stockQtyAvailable: item.stockQtyAvailable, warehouseId: item.warehouseId,
                      storageLocationId: item.storageLocationId, batchNo: item.batchNo, projectId: item.projectId}
                  let data = Object.assign({}, record, stockData);
                  data.salesOrderDetailId = data.id;
                  data.salesOrderId = data.mainId;
                  data.id = data.id + '' + index
                  dataArr.push(data)
                }
              })
            }
          )
        }else{
          this.$message.warning(res.message)
        }
        that.dataSource = dataArr;
      }).finally(() => {
        this.loading = false
      })
    },
    handleOk(){
      let table = this.$refs.sdSalesDetailOutTable;
      let {error, values} = table.getValuesSync({validate: true})
      if(error === 0){
        //校验通过
        this.loading = true
        let url = this.url.handleOut
        let datas = values.filter(item => item.outQty > 0)
        putAction(url, datas).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.$emit('ok')
          } else {
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      }
    },
  }
}
</script>

<style scoped>
.action-button {
  margin-bottom: 8px;
}

.gap {
  padding-left: 8px;
}
</style>